package com.example.fangyh09.viewflipper;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.MotionEvent;
import android.widget.ImageView;
import android.widget.ViewFlipper;

public class MainActivity extends AppCompatActivity {

    ViewFlipper flipper;
    int[] resId = {R.drawable.pic1,R.drawable.pic2,R.drawable.pic3,R.drawable.pic4};
    float startX;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        flipper = (ViewFlipper)findViewById(R.id.flipper);
        for (int i = 0;i < resId.length;i ++) {
            flipper.addView(getImageView(resId[i]));
        }
        flipper.setInAnimation(this,R.anim.left_in);
        flipper.setOutAnimation(this, R.anim.left_out);
        flipper.setFlipInterval(3000);
        flipper.startFlipping();
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        //return super.onTouchEvent(event);
        switch(event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                startX = event.getX();
                break;
            case MotionEvent.ACTION_MOVE:
                if (event.getX() - startX > 100){
                    flipper.setInAnimation(this,R.anim.left_in);
                    flipper.setInAnimation(this,R.anim.left_out);
                    flipper.showPrevious();
                }

                //left
                if (startX - event.getX() > 100) {
                    flipper.setInAnimation(this,R.anim.right_in);
                    flipper.setInAnimation(this,R.anim.right_out);
                    flipper.showNext();
                }
                break;
            case MotionEvent.ACTION_UP:
                break;
        }
        return super.onTouchEvent(event);
    }

    ImageView getImageView(int resId) {
        ImageView image = new ImageView(this);
        //image.setImageResource(resId);
        image.setBackgroundResource(resId);
        return image;
    }
}
















































